package com.hycc.base;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class SynchronousQueueDemo {
    public static void main(String[] args) {
        //同步队列
        BlockingQueue<String> bq = new SynchronousQueue<>();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+" put "+1);
                bq.put(String.valueOf(1));
                System.out.println(Thread.currentThread().getName()+" put "+2);
                bq.put(String.valueOf(2));
                System.out.println(Thread.currentThread().getName()+" put "+3);
                bq.put(String.valueOf(3));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"ta").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName()+" take "+bq.take());
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName()+" take "+bq.take());
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName()+" take "+bq.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"tb").start();

    }
}
